package test.repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import test.domain.Organization;
import test.domain.QOrganization;
import test.domain.QUser;

import com.mysema.query.jpa.impl.JPAQuery;

public class OrganizationRepositoryImpl implements OrganizationRepositoryCustom {
    @PersistenceContext
    private EntityManager em;

    @Override
    public Organization findBestOrg() {
        return new JPAQuery(em).from(QOrganization.organization)
                               .join(QOrganization.organization.users, QUser.user)
                               .groupBy(QOrganization.organization)
                               .orderBy(QUser.user.salary.avg().desc())
                               .limit(1)
                               .uniqueResult(QOrganization.organization);
    }
}
